<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CouponStoreRequest;
use App\Models\Coupon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class CouponController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = Coupon::filter(\request()->all())->orderByRaw('id desc')->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param CouponStoreRequest $request
     * @return JsonResponse|object
     */
    public function store(CouponStoreRequest $request)
    {
        $params = $request->validated();
        $model = Coupon::create($params);

        return $this->restful($model);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param CouponStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(CouponStoreRequest $request, int $id)
    {
        $params = $request->validated();
        $model = Coupon::where('id', $id)->update($params);
        return $this->restful($model);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        $model = Coupon::where('id', $id)->delete();

        return $this->restful($model);
    }

    public function getOptions()
    {
        $list = Coupon::select(['id as value', 'name as label'])
            ->where('is_active', 1)
            ->where('stop_get', '>', now())
            ->get()
            ->toArray();
        return $this->restful($list);
    }
}
